import jofc2.model.*
import jofc2.model.elements.LineChart
import jofc2.model.axis.YAxis
import jofc2.model.axis.XAxis
import jofc2.model.axis.Label
import jofc2.model.elements.AreaHollowChart
import jofc2.model.elements.PieChart
import jofc2.model.axis.Label.Rotation
import jofc2.model.elements.BarChart
import jofc2.OFC
import jofc2.model.elements.FilledBarChart
import jofc2.model.elements.SketchBarChart
import jofc2.model.elements.HorizontalBarChart
import jofc2.model.elements.ScatterChart
import java.math.MathContext
import jofc2.model.elements.StackedBarChart
import jofc2.model.elements.StackedBarChart.StackValue
import jofc2.model.elements.Legend

class QualityControlController {

    def index = { }


    // 班级教学质量，显示班级类别，点进入是班级的考试纪录
    def classes = {
        params.sort = 'grade'
        params.order = 'asc'
 
        [classesInstanceList: Classes.findAllBySchoolAndEnable(session.school, true, params) ,
                classesInstanceTotal: Classes.countByEnable(true)]
        
    }

    def classesExam = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        def classes = Classes.get(params.id)
        if (classes) { 
               [examInstanceList: Exam.findAllByClasses(classes ,  params), 
                examInstanceTotal: Exam.countByClasses(classes ) , 
                classes : classes ,
                course : params.course] 
        } else{
            [examInstanceList : [] , examInstanceTotal : 0]
        }
    }
    // 学生成绩分析，显示某个班，每个学生的成绩图表
    def student = {
        params.sort = 'grade'
        params.order = 'asc'
 
        [classesInstanceList: Classes.findAllBySchoolAndEnable(session.school, true, params) , 
                classesInstanceTotal: Classes.countByEnable(true)]
        
    }

    def studentList = {
        params.sort = 'name'
        params.order = 'asc'

        def classes = Classes.get(params.id)
        if (classes) { 
            [studentInstanceList: Student.findAllByClasses(classes , params) ,
                classes : classes ,
                course : params.course]
        }else{
             [studentInstanceList: []]
        }
    }

    

    def passingRate = {
        
    }

    def passingRateChart = {
 
      
            if (params.order) {
                params.sort = 'score'
            } 
            def classes = Classes.findAllBySchoolAndEnable(session.school, true, params) 

            def chart = new Chart("${session.school}及格率评比").setYAxis(new YAxis().setRange(0, 100, 10)).setBackgroundColour("#ECF9FF");
 
            def xaxis = new XAxis().setTickHeight(10).set3D(3).setColour("#909090");
            //def line = new LineChart(LineChart.Style.HOLLOW);  
            def element = new BarChart(BarChart.Style.THREED).setColour("#5579FF")
            //element.setHaloSize(0).setWidth(2).setDotSize(4);
  
            classes.each { classx -> 
                def bar
                def value = 40 + Math.round((Math.random()*60))
                if (value >= 80) {// 优秀
                    bar = new BarChart.Bar(value, "#3333FF") 
                }else if (value >= 60){//及格
                    bar = new BarChart.Bar(value) 
                }else{// 不及格
                    bar = new BarChart.Bar(value, "#FF6666") 
                }
                bar.setTooltip("${classx}<br>及格率${value}%")
                element.addBars(bar)
                xaxis.addLabels(new Label(classx?.toString()))  
            }
 
            chart.setXAxis(xaxis).addElements(element)
            //.setXLegend(new Text("tests", Text.TEXT_ALIGN_CENTER ) )
 
            //        println chart.toDebugString()
            return render(chart)
       
    }  

    def average = {
        
    }

    def averageChart = {
                    if (params.order) {
                params.sort = 'score'
            } 
            def classes = Classes.findAllBySchoolAndEnable(session.school, true, params) 

            def chart = new Chart("${session.school}平均分评比").setYAxis(new YAxis().setRange(0, 100, 10)).setBackgroundColour("#ECF9FF");
 
            def xaxis = new XAxis().setTickHeight(10).set3D(3).setColour("#909090");
            //def line = new LineChart(LineChart.Style.HOLLOW);  
            def element = new BarChart(BarChart.Style.THREED).setColour("#5579FF")
            //element.setHaloSize(0).setWidth(2).setDotSize(4);
  
            classes.each { classx -> 
                def bar
                def value = 40 + Math.round((Math.random()*60))
                if (value >= 80) {// 优秀
                    bar = new BarChart.Bar(value, "#3333FF") 
                }else if (value >= 60){//及格
                    bar = new BarChart.Bar(value) 
                }else{// 不及格
                    bar = new BarChart.Bar(value, "#FF6666") 
                }
                bar.setTooltip("${classx}<br>平均分：${value}分")
                element.addBars(bar)
                xaxis.addLabels(new Label(classx?.toString()))  
            }
 
            chart.setXAxis(xaxis).addElements(element)
            //.setXLegend(new Text("tests", Text.TEXT_ALIGN_CENTER ) )
 
            //        println chart.toDebugString()
            return render(chart)
    }
}
